package edu.zju.gis.dbfg.model.tile;

import edu.zju.gis.dbfg.common.exception.ValidationException;
import lombok.Getter;
import lombok.Setter;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.Option;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;

/**
 * @author Hu
 * @date 2019/9/4
 * VectorTileClipper模型的运行参数定义类
 **/
@Getter
@Setter
public class VectorTileClipperArgs implements Serializable {

  private final static Logger logger = LoggerFactory.getLogger(VectorTileClipperArgs.class);

  @Option(name = "-input",usage = "输入数据", required = true)
  private String input;

  @Option(name = "-outdir",usage = "瓦片数据存放路径", required = true)
  private String outDir;

  @Option(name = "-layername",usage = "图层名称", required = true)
  private String layerName;

  @Option(name = "-sourceExtent",usage = "图层坐标系空间范围, 格式为: xmin, xmax, ymin, ymax")
  private String sourceExtent = "-180, 180, -90, 90"; // 4326
  // private String extent = "-20026376.39, 20026376.39, -20048966.10, 20048966.10"; // epsg.io 3857
  // private String sourceExtent = "-20037508.3427892, 20037508.3427892, -20048966.1040146, 20048966.1040145"; // geoserver 3857


  @Option(name = "-extent",usage = "图层坐标系空间范围, 格式为: xmin, xmax, ymin, ymax")
  // private String extent = "-180, 180, -90, 90"; // 4326
  private String extent = "-20026376.39, 20026376.39, -20048966.10, 20048966.10"; // epsg.io 3857
  // private String extent = "-20037508.3427892, 20037508.3427892, -20048966.1040146, 20048966.1040145"; // geoserver 3857

  @Option(name = "-scrs",usage = "原始数据坐标系")
  // private String sourceCRS = "GEOGCS[\"China Geodetic Coordinate System 2000\",DATUM[\"D_China_2000\",SPHEROID[\"CGCS2000\",6378137,298.257222101]],PRIMEM[\"Greenwich\",0],UNIT[\"Degree\",0.017453292519943295]]";
  // private String sourceCRS = "EPSG: 4326";
  private String sourceCRS = "GEOGCS[\"GCS_WGS_1984\", \n" +
          "  DATUM[\"D_WGS_1984\", \n" +
          "    SPHEROID[\"WGS_1984\", 6378137.0, 298.257223563]], \n" +
          "  PRIMEM[\"Greenwich\", 0.0], \n" +
          "  UNIT[\"degree\", 0.017453292519943295], \n" +
          "  AXIS[\"Longitude\", EAST], \n" +
          "  AXIS[\"Latitude\", NORTH]]";

  @Option(name = "-tcrs",usage = "瓦片数据坐标系")
  private String targetCRS = "PROJCS[\"WGS_1984_Web_Mercator_Auxiliary_Sphere\", \n" +
          "  GEOGCS[\"GCS_WGS_1984\", \n" +
          "    DATUM[\"D_WGS_1984\", \n" +
          "      SPHEROID[\"WGS_1984\", 6378137.0, 298.257223563]], \n" +
          "    PRIMEM[\"Greenwich\", 0.0], \n" +
          "    UNIT[\"degree\", 0.017453292519943295], \n" +
          "    AXIS[\"Longitude\", EAST], \n" +
          "    AXIS[\"Latitude\", NORTH]], \n" +
          "  PROJECTION[\"Popular Visualisation Pseudo Mercator\"], \n" +
          "  PARAMETER[\"semi_minor\", 6378137.0], \n" +
          "  PARAMETER[\"latitude_of_origin\", 0.0], \n" +
          "  PARAMETER[\"central_meridian\", 0.0], \n" +
          "  PARAMETER[\"scale_factor\", 1.0], \n" +
          "  PARAMETER[\"false_easting\", 0.0], \n" +
          "  PARAMETER[\"false_northing\", 0.0], \n" +
          "  UNIT[\"m\", 1.0], \n" +
          "  AXIS[\"x\", EAST], \n" +
          "  AXIS[\"y\", NORTH]]";

  @Option(name = "-zmin",usage = "瓦片金字塔范围最小值")
  private int zMin = 8;

  @Option(name = "-zmax",usage = "瓦片金字塔范围最大值")
  private int zMax = 12;

  @Option(name = "-jobExtent", usage = "所有数据的空间范围，默认中国国土四至，必须为 4326")
  // private String jobExtent = "73, 136, 3, 54";
  private String jobExtent = "115.25,117.30,39.26,41.03";

  @Option(name = "-jobSplit", usage = "任务切割指定层级")
  private int jobSplitZLevel = 8;

  @Option(name = "-fileType", usage = "瓦片类型")
  private String fileType = "mvt";

  @Option(name = "-qgWktFile", usage = "瓦片类型")
  // private String qgWktFile = "C:\\Users\\ZJU\\qgwkt.txt";
  private String qgWktFile="C:\\Users\\ZJU\\bjwkt.txt";

  public  static VectorTileClipperArgs initArgs(String args[]){
    VectorTileClipperArgs mArgs = new VectorTileClipperArgs();
    CmdLineParser parser = new CmdLineParser(mArgs);
    try {
      parser.parseArgument(args);
      if (!mArgs.checkValid()){
        throw new ValidationException(VectorTileClipperArgs.class,"initArgs()", "Unvalid arguments", args);
      }
      return mArgs;
    } catch (CmdLineException e) {
      logger.error(e.getLocalizedMessage());
      return null;
    }
  }

  private boolean checkValid() {
    boolean flag = true;
    flag = flag && (zMin >= 0);
    flag = flag && (zMax <= 20);
    flag = flag && (jobSplitZLevel <= zMax);
    flag = flag && (jobSplitZLevel >= zMin);
    return flag;
  }

}
